/**
 * 
 */
package barney.davis.hume.it3048todo.dao;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;

import barney.davis.hume.it3048todo.constants.TaskRepeat;
import barney.davis.hume.it3048todo.constants.TaskStatus;
import barney.davis.hume.it3048todo.dto.Task;

/**
 * @author Adam
 * 
 */
public class TaskDAOStub implements ITaskDAO {

	/**
	 * {@inheritDoc} This stub implementation requires, at minimum, a due date.
	 * All other fields in task may be empty.
	 * 
	 * @throws IllegalArgumentException
	 *             Requires a valid due date.
	 */
	@Override
	public void save(Task task) throws Exception {
		// TODO Auto-generated method stub

		if ((task.getDueDate().equals("")) || (task.getDueDate() == null)) {
			throw new IllegalArgumentException("Due date cannot be empty");
		}
	}

	/**
	 * This stub implementation returns sample task A, unless the ID equals
	 * "BadID", or is empty, in which case it throws an
	 * InvalidArgumentException.
	 * 
	 * The production implementation will throw an exception when the ID does
	 * not match any IDs in the database.
	 * 
	 * {@inheritDoc}
	 */
	@Override
	public Task fetch(String ID) throws Exception {

		Task returnTask = getTaskA();
		if ((ID.equals("") || ID == null)) {
			throw new IllegalArgumentException("ID cannot be empty");
		} else if (ID.equals("BadID")) {
			throw new IllegalArgumentException(
					"ID BadID is not a valid identification");
		} else
			return returnTask;

	}

	/**
	 * {@inheritDoc}
	 * 
	 * This stub implementation returns a hashmap with just one task (sample
	 * task A) when the status is INPROGRESS, and a hashmap with just one task
	 * (sampe task B) when the status is OVERDUE. Throws an exception for all
	 * other statuses.
	 * 
	 */
	@Override
	public ArrayList<Task> fetchByStatus(TaskStatus status)
			throws Exception {
		if (status.equals(TaskStatus.INPROGRESS)) {
			Task task = getTaskA();
			ArrayList<Task> taskList = new ArrayList<Task>();
			taskList.add(task);
			return taskList;
		} else if (status.equals(TaskStatus.OVERDUE)) {
			Task task = getTaskB();
			ArrayList<Task> taskList = new ArrayList<Task>();
			taskList.add(task);
			return taskList;
		} else {
			throw new IllegalArgumentException(
					"No taskList with the supplied status were found");
		}
	}

	/**
	 * This stub implementation returns a hashmap with just one task (sample
	 * task A) when the date is 2013-11-10-17-15, or a hashmap with just one
	 * task (sample task B) when the date is 2013-6-2-22-0. All other dates
	 * throw an IllegalArgumentException.
	 * 
	 * {@inheritDoc}
	 */
//	@Override
//	public ArrayList<Task> fetchByDueDate(Calendar date) throws Exception {
//		if (date.equals(new GregorianCalendar(2013, 11, 10, 17, 15))) {
//			Task task = getTaskA();
//			ArrayList<Task> taskList = new ArrayList<Task>();
//			taskList.add(task);
//			return taskList;
//		} else if (date.equals(new GregorianCalendar(2013, 6, 2, 22, 0))) {
//			Task task = getTaskB();
//			ArrayList<Task> taskList = new ArrayList<Task>();
//			taskList.add(task);
//			return taskList;
//		} else {
//			throw new IllegalArgumentException(
//					"No taskList with the supplied due date were found");
//		}
//	}

	/**
	 * Returns a HashMap with both Sample Task A and Sample Task B. Throws
	 * exception when there are no taskList.
	 * 
	 * {@inheritDoc}
	 */
	@Override
	public ArrayList<Task> fetchAll() throws Exception {
		Task taskA = getTaskA();
		Task taskB = getTaskB();
		ArrayList<Task> taskList = new ArrayList<Task>();
		taskList.add(taskA);
		taskList.add(taskB);
		return taskList;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void delete(Task task) throws Exception {

	}

	private static Task getTaskA() {
		Task task = new Task();
		task.setText("Take the dog to the park");
		task.setDueDate(new GregorianCalendar(2013, 11, 10, 17, 15));
		task.setStatus(TaskStatus.COMPLETE);
		task.setRepeat(TaskRepeat.NONE);
		task.setComplexity(3);
		return task;

	}

	private static Task getTaskB() {
		Task task = new Task();
		task.setText("Watch Mad Men");
		task.setDueDate(new GregorianCalendar(2013, 6, 2, 22, 0));
		task.setRepeat(TaskRepeat.WEEKDAYS);
		task.setStatus(TaskStatus.OVERDUE);
		task.setComplexity(40);
		return task;
	}

	@Override
	public ArrayList<Task> fetchByDueDate(Calendar firstDate, Calendar lastDate) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
